#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qpixmap.h"
#include <qstate.h>
#include "qstatemachine.h"
#include "qdesktopwidget.h"
#include "qprocess.h"
#include "qtextcodec.h"
#include "qchar.h"
#include <QTime>
#include "qlabel.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "qthread.h"
#include <QDate>

unsigned char CalculateXOR(unsigned char *pbuf,int len)
{
  int i;
  unsigned char Xor;

  Xor = 0;
  for(i=0;i<len;i++)
  {
    Xor ^= pbuf[i];
  }
  return Xor;
}


void encryption_check(unsigned char *psrc,unsigned char *pdes)
{
int i;
unsigned char val;
unsigned char buf[32];

memset(buf,0x00,32);

for(i=0;i<24;i++)
{
buf[i] = (unsigned char)((psrc[i]^0x5a)*1.17);
}

for(i=0;i<12;i++)
{
val=buf[i];
buf[i] = buf[23-i];
buf[23-i] = val;
}
memcpy(pdes,buf,24);
}


unsigned char MainWindow::system_verify()
{
    int uart_fd_1;
    int tt;
    unsigned char src[16];
    unsigned char des[16];
    uart_fd_1 = Init_serial(elevator_info_uart_1);
    if(uart_fd_1 < 0)
    {
        qDebug("open encrypt uart fail");
        return 0;
    }

    QTime time00;
    time00= QTime::currentTime();
    qsrand(time00.msec());
    tt=qrand();
    qDebug("msec:0x%x",tt);
    qsrand(time00.second());
    tt=qrand();
    qDebug("msec:0x%x",tt);

}

void MainWindow::update_uart_context()
{

    qint32 t_num;
    uart_context_flag = 1;
    uart_context = text_uart_thread->uart_context;
    ui->label_1->setText(uart_context);
    t_num = ui->label_1->text().size();
    //qDebug("the number is :%d",t_num);
    //ui->label_1->setFixedSize(screenrect.height()/ratio_y*t_num,screenrect.height()/ratio_y);
}


void MainWindow::update_floor()
{
    QString name_floor;
    QString fonts_size;

    qint32 tt = (elevator_info_state1.floor_info&0xff);
    qint32 pp = 0;
    //QPalette  palette00;

    if(elevator_info_state1.floor_info !=0)
    {
    if(elevator_info_state1.floor_info > 0x80)
    {
        if(((5-tt) < floor_real_name.count())&&((5-tt)>0))
        {
            name_floor = floor_real_name.at(5-tt);
        }
         else
            name_floor = " ";
        //t2.sprintf("B%d",elevator_info_state1.floor_info&0x0f);


    }
    else
    {
        if((tt+4) < floor_real_name.count())
            name_floor = floor_real_name.at(tt + 4);
        else
            name_floor = " ";
    }
       // t2.sprintf("%d",elevator_info_state1.floor_info);
    //process0->write("pause \n");
    if(name_floor.count()<3)
    {
        fonts_size.sprintf("font-size : %dpx",label_floor->geometry().height()-4);
        label_floor->setStyleSheet(fonts_size);
    }
    else
    {
        name_floor = name_floor.left(3);
        pp =1;
        int t_num;
        while(1)
        {

        fonts_size.sprintf("font-size : %dpx",label_floor->geometry().height()*2/3 -pp);
        label_floor->setStyleSheet(fonts_size);
        t_num = label_floor->fontMetrics().width(name_floor);
        if(t_num < label_floor->geometry().width())
            break;
        else
            pp++;

        }

    }
    label_floor->setText(name_floor);
    //QPalette palette_floor;
    //palette_floor = label_floor->palette();
    //palette_floor.setColor(QPalette::WindowText,QColor("yellow"));
    label_floor->setPalette(palette_yellow);
    label_floor->show();
    }
    else
        label_floor->hide();


}

void MainWindow::update_floor2()
{
    QString name_floor;
    QString fonts_size;    //qint32 tt;
    //QPalette  palette00;
    qint32 pp = 0;

    qint32 tt = (elevator_info_state2.floor_info&0xff);
    //QPalette  palette00;

    if(elevator_info_state2.floor_info !=0)
    {
    if(elevator_info_state2.floor_info > 0x80)
    {
        if(((5-tt) < floor_real_name.count())&&((5-tt)>0))
        {
            name_floor = floor_real_name.at(5-tt);
        }
         else
            name_floor = " ";
        //t2.sprintf("B%d",elevator_info_state1.floor_info&0x0f);


    }
    else
    {
        if((tt+4) < floor_real_name.count())
            name_floor = floor_real_name.at(tt + 4);
        else
            name_floor = " ";
    }
       // t2.sprintf("%d",elevator_info_state1.floor_info);
    //process0->write("pause \n");
    if(name_floor.count()<3)
    {
        fonts_size.sprintf("font-size : %dpx",label_floor1->geometry().height()-4);
        label_floor1->setStyleSheet(fonts_size);
    }
    else
    {
        int t_num;
        name_floor = name_floor.left(3);
        while(1)
        {
            fonts_size.sprintf("font-size : %dpx",label_floor1->geometry().height()*2/3-pp);
            label_floor1->setStyleSheet(fonts_size);
            t_num = label_floor1->fontMetrics().width(name_floor);
            if(t_num < label_floor1->geometry().width())
                break;
            else
                pp++;

        }

    }
    label_floor1->setText(name_floor);
    //QPalette palette_floor;
    //palette_floor = label_floor->palette();
    //palette_floor.setColor(QPalette::WindowText,QColor("yellow"));
    label_floor1->setPalette(palette_yellow);
    label_floor1->show();
    }
    else
        label_floor1->hide();



#if 0
    if(elevator_info_state2.floor_info !=0)
    {
        #if 1
    if(elevator_info_state2.floor_info > 0x80)
        t2.sprintf("B%d",elevator_info_state2.floor_info&0x0f);
    else
        t2.sprintf("%d",elevator_info_state2.floor_info);
    //process0->write("pause \n");
    label_floor1->setText(t2);
#endif
    //QPalette palette_floor;
    //palette_floor = label_floor1->palette();
    //palette_floor.setColor(QPalette::WindowText,QColor("yellow"));
    label_floor1->setPalette(palette_yellow);
    label_floor1->show();
    }
    else
        label_floor1->hide();
#endif
}



void MainWindow::update_spec_info()
{

/*第0位为1时表示暂停服务，第1位为1时表示管制，第2位为1时表示超载，其它位为保留备用位，上述位为0时表示无特殊状态信息：*/


    if(elevator_info_state1.spec_info & 0x01)
        label_spec_info->setText(tr("暂停"));
     else if(elevator_info_state1.spec_info & 0x02)
        label_spec_info->setText(tr("管制"));
    else  if(elevator_info_state1.spec_info & 0x04)
        label_spec_info->setText(tr("超载"));
    else  if(elevator_info_state1.spec_info & 0x06)
        label_spec_info->setText(tr("管制 超载"));
    else if ((elevator_info_state1.spec_info & 0xf0) == 0x10)
        label_spec_info->setText(tr("连接失败"));
    else
        label_spec_info->setText(tr(""));

    label_spec_info->setPalette(palette_red);
    label_spec_info->show();
}

void MainWindow::update_spec_info2()
{

/*第0位为1时表示暂停服务，第1位为1时表示管制，第2位为1时表示超载，其它位为保留备用位，上述位为0时表示无特殊状态信息：*/

    if(elevator_info_state2.spec_info & 0x01)
        label_spec_info1->setText(tr("暂停"));
     else if(elevator_info_state2.spec_info & 0x02)
        label_spec_info1->setText(tr("管制"));
    else  if(elevator_info_state2.spec_info & 0x04)
        label_spec_info1->setText(tr("超载"));
    else  if(elevator_info_state2.spec_info & 0x06)
        label_spec_info1->setText(tr("管制 超载"));
    else
        label_spec_info1->setText(tr(""));


    label_spec_info1->setPalette(palette_red);
    label_spec_info1->show();
}

void MainWindow::roll_disposal()
{

    QString line;
    qint32 t_num;
    line = top_lines.at(read_roll_para.current_line);

    ui->label->setText(line);
    t_num = ui->label->text().size();
    ui->label->setText("");
    //qDebug("the number is :%d",t_num);
    ui->label->setFixedSize(50*t_num,50);
    read_roll_para.current_line++;
    if(read_roll_para.current_line>=read_roll_para.line_num)
        read_roll_para.current_line = 0;
}

QString MainWindow::get_next_bottom_line()
{
    QString line;
    line = bottom_lines.at(read_roll_bottom.current_line);
    line = line + "           ";

    read_roll_bottom.current_line++;
    if(read_roll_bottom.current_line>=read_roll_bottom.line_num)
        read_roll_bottom.current_line = 0;
    return line;
}

void MainWindow::roll_disposal2()
{
#if 1
    QString line = "";
    qint32 t_num = 0;
   // qDebug("current_line:%d,line_num:%d",read_roll_bottom.current_line,read_roll_bottom.line_num);
    //ui->label_1->setStyleSheet("background-color: red");

    //line = bottom_lines.at(read_roll_bottom.current_line);
    //line += "           ";

    if(((ui->label->geometry().topLeft().x()>screenrect.width())&&(ui->label_1->geometry().topLeft().x()>screenrect.width())))
        {
            do
            {
                line += get_next_bottom_line();

                ui->label->setText(line);
                qDebug("%s", qPrintable(line));
                //t_num = ui->label_1->text().size();
                t_num = ui->label->fontMetrics().width(line);
                //t_num = ui->label_1->fontMetrics().width(line);

                //qDebug("the string size %dpx",t_num);
            }while(t_num < screenrect.width());
            ui->label->setFixedSize(t_num,screenrect.height()/ratio_y);
            ui->label->show();

            ui->label_1->move(ui->label->geometry().topRight());
            line = "";
            do
            {
                line += get_next_bottom_line();
                ui->label_1->setText(line);

                //t_num = ui->label_1->text().size();
                t_num = ui->label_1->fontMetrics().width(line);
                //t_num = ui->label_1->fontMetrics().width(line);

                //qDebug("the string size %dpx",t_num);
            }while(t_num < screenrect.width());
            ui->label_1->setFixedSize(t_num,screenrect.height()/ratio_y);
            ui->label_1->show();



        }
        else
        {
            if(((ui->label->geometry().topLeft().x()) > (ui->label_1->geometry().topLeft().x())))
            {
                do
                {
                    line += get_next_bottom_line();
                    ui->label->setText(line);
                    //t_num = ui->label_1->text().size();
                    t_num = ui->label->fontMetrics().width(line);
                    //t_num = ui->label_1->fontMetrics().width(line);

                    //qDebug("the string size %dpx",t_num);
                }while(t_num < screenrect.width());
                ui->label->setFixedSize(t_num,screenrect.height()/ratio_y);

              }
            else
            {
                do
                {
                    line += get_next_bottom_line();
                    ui->label_1->setText(line);
                    //t_num = ui->label_1->text().size();
                    t_num = ui->label_1->fontMetrics().width(line);
                    //t_num = ui->label_1->fontMetrics().width(line);

                    //qDebug("the string size %dpx",t_num);
                }while(t_num < screenrect.width());
                ui->label_1->setFixedSize(t_num,screenrect.height()/ratio_y);

            }
        }
         qDebug("%s", qPrintable(line));


    #endif
}

void MainWindow::get_floor_real_name()
{

    int nn = 0;
    QString tmp_line0 = "/file/floor_name.txt";
    tmp_line0 = current_dir + tmp_line0;
    floor_real_name.clear();
    QFile file2(tmp_line0);
     if ( file2.open(QIODevice::ReadOnly) ) {
           QTextStream stream( &file2);
           QString line;
           QChar *char0;
         stream.setCodec("GBK");
#if 1
         while ( !stream.atEnd()) {
             line = stream.readLine(); // 不包括“\n”的一行文
             line = line.remove(" ");
             //qDebug("line %s",line.at(0).toAscii());
             if(line == "")
                 continue;
             if(line.count() == 0)
                 continue;
             char0 = line.data();
             if((*char0 == '#') || (*(char0+1) == '#'))
                   continue;
             else
             {
                floor_real_name += line;
                nn++;
             }
         }
#endif
         qDebug("floor name number:%d ",floor_real_name.count());
         file2.close();
     }
     else
     {
            int tt=0;
            QString tmp00;
            for(tt = 0; tt< 99;tt++)
            {
                if(tt==0)
                    floor_real_name += "B5";
                else if(tt==1)
                    floor_real_name += "B4";
                else if(tt==2)
                    floor_real_name += "B3";
                else if(tt==3)
                    floor_real_name += "B2";
                else if(tt==1)
                    floor_real_name += "B1";
                else
                {
                    tmp00.sprintf("%d",tt-4);
                    floor_real_name += tmp00;
                }


            }

     }


}

void MainWindow::read_caption_file()
{

    int n = 0;
    //QStringList lines;

    QString tmp_line0 = "../file/caption_top.txt";
    //tmp_line0 = current_dir + tmp_line0;
    QFile file_top(tmp_line0);
     if ( file_top.open(QIODevice::ReadOnly) ) {
         QTextStream stream( &file_top );
         QString line;

         stream.setCodec("GBK");
         line = stream.readLine();
         if(line == "北京艾乐维特科技有限公司")
         {

             while ( !stream.atEnd()) {
                 line = stream.readLine(); // 不包括“\n”的一行文本
                 n++;
                 top_lines += line;
             }
         }
         else
         {

             file_top.close();
             file_top.open(QIODevice::ReadOnly);
             QTextStream stream1( &file_top );
             stream.setCodec("UTF-8");
             line = stream1.readLine();

             if(line == "北京艾乐维特科技有限公司")
             {

                 while ( !stream.atEnd()) {
                     line = stream.readLine(); // 不包括“\n”的一行文本
                     n++;
                     top_lines += line;
                 }
             }
             else
             {
                 line = tr("文本信息错误");
                 top_lines += line;
                 n =1;
             }
         }
     }
     else
       {
       qDebug("open caption fail!\n");
       QString line;
       line = tr("没有文本信息");
       top_lines += line;
       n =1;
     }
     file_top.close();


     read_roll_para.current_line = 0;
     read_roll_para.current_times = 0;
     read_roll_para.line_num = n;
     emit(emit_roll_one_time());

#if 1
     bottom_lines.clear();
     n=0;
     tmp_line0 = "/file/caption_bottom.txt";
     tmp_line0 = current_dir + tmp_line0;
     QFile file1(tmp_line0);
      if ( file1.open(QIODevice::ReadOnly) ) {
          QTextStream stream_bottom_1( &file1 );
          QString line;
          stream_bottom_1.setCodec("GBK");
          line = stream_bottom_1.readLine();
          qDebug("%s", qPrintable(line));

          if(line == tr("北京艾乐维特科技有限公司"))
          {

              while ( !stream_bottom_1.atEnd()) {
                  line = stream_bottom_1.readLine(); // 不包括“\n”的一行文本
                  n++;
                  bottom_lines += line;
              }
          }
          else
          {

              file1.close();
              file1.open(QIODevice::ReadOnly);
              QTextStream stream_bottom_2( &file1 );
              stream_bottom_2.setCodec("UTF-8");
              line = stream_bottom_2.readLine();
              qDebug("%s", qPrintable(line));



              if(line == tr("北京艾乐维特科技有限公司"))
              {

                  while ( !stream_bottom_2.atEnd()) {
                      line = stream_bottom_2.readLine(); // 不包括“\n”的一行文本
                      n++;
                      bottom_lines += line;
                  }
              }
              else
              {
                  line = tr("文本信息错误2");
                  bottom_lines += line;
                  n =1;
              }
          }
          file1.close();

#if 0
          while ( !stream.atEnd()) {
              line = stream.readLine(); // 不包括“\n”的一行文本
              n++;
              bottom_lines += line;
          }
          file.close();
#endif
      }
      else
        {
        qDebug("open caption fail!\n");
        QString line;
        line = tr("没有文本信息");
        bottom_lines += line;
        n =1;
      }

      read_roll_bottom.current_line = 0;
      read_roll_bottom.current_times = 0;
      read_roll_bottom.line_num = n;
     //emit(emit_roll_one_time_bottom());
      ui->label->show();
      ui->label_1->show();
      QTimer::singleShot(500,this,SLOT(roll_disposal2()));
#endif

     tmp_line0 = "/file/video_list.txt";
     tmp_line0 = current_dir + tmp_line0;
     video_lines.clear();
     QFile file2(tmp_line0);
      if ( file2.open(QIODevice::ReadOnly) ) {
            QTextStream stream( &file2);
            QString line;
            QChar *char0;
          stream.setCodec("GBK");
 #if 1
          while ( !stream.atEnd()) {
              line = stream.readLine(); // 不包括“\n”的一行文
              line = line.remove(" ");
              //qDebug("line %s",line.at(0).toAscii());
              if(line == "")
                  continue;
              char0 = line.data();
              if((*char0 == '#') && (*(char0+1) == '#'))
                    continue;
              else
              {
                  line = current_dir + "/video/" + line;

                    QFileInfo info0(line);
                    if(info0.exists() == TRUE)
                    {
                    video_lines += line;

                    video_files += " " + line;
                    }
                    else
                        qDebug("video file not exist");
              }
          }
 #endif
          qDebug("video_list number:%d ",video_lines.count());
          file2.close();
      }
      else
        qDebug("open video_list fail!\n");
}


void MainWindow::read_fifo()
{
    //qint32 fifo_fd;
   // char *mychar = "333333333333";
    QDate date0;// = new QDate;
    QString tt0 = date0.currentDate().toString("yyyy.MM.dd  ddd");
    QString tt;
    //tt = gbCode->toUnicode(temp);

    //ui->label->setText(tt);

   // label_floor->setText(tt);
    //fifo_fd = open("testfifo",O_RDONLY);


}

TextUartThread::TextUartThread()
{
    wait();
}

void TextUartThread::run()
{
    unsigned char tt;
    int numBytes;

    int real_num;


    //port = new QextSerialPort("/dev/ttyUSB0");
    QFileInfo info0(text_uart);
    if(info0.exists())
        port = new QextSerialPort(text_uart);
    else
        port = new QextSerialPort("/dev/ttyUSB0");
    port->setBaudRate(BAUD115200);
    port->setFlowControl(FLOW_OFF);
    port->setParity(PAR_NONE);

    port->setDataBits(DATA_8);
    port->setStopBits(STOP_2);
    //set timeouts to 500 ms
    port->setTimeout(500);

    port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
    qDebug("is open: %d", port->isOpen());

    buff_out[0] = 0xaa;
    buff_out[1] = 0x55;
    while(1)
    {
        memset(buff_in,0,1024);
        numBytes = port->bytesAvailable();
        //qDebug("bytes available: %d", numBytes);
        if(numBytes > 0)
         {
        if(numBytes > 256)
            numBytes = 256;

        real_num = port->read(buff_in, numBytes);

        if(real_num <= 0)
            continue;
        //char hanzi[32]={0xCE,0xE4,0xBA,0xBA,0xCA,0xD0 ,0xB6,0xAB ,0xBA,0xFE ,0xB8,0xDF ,0xD0,0xC2 ,0xC7,0xF8 ,0xBD,0xF0 ,0xC8,0xDA ,0xB8,0xDB};
        //QString tmp;
        QByteArray ba0(buff_in,real_num);
        //ba0.resize(real_num);
        //ba0.data()=buffer0;
        //QByteArray encodedString = "xxx";
        QTextCodec *codec = QTextCodec::codecForName("GBK");
        QString string0 = codec->toUnicode(ba0);
        //text_uart = string0;
        emit(emit_update());

        qDebug("bytes available: %d", numBytes);
    }

         msleep(50);
    }
}



int Init_serial(QString u_serial)
{

    struct termios opt;
    int fd =-1 ;
    char *p;

    QFileInfo info0(u_serial);
    if(info0.exists())
    {
        QByteArray ba=u_serial.toAscii();
        p=ba.data();
        fd = open(p, O_RDWR | O_NOCTTY);
    }


    if(fd == -1)
    {
        qDebug("open serial fail   !");
    }

    tcgetattr(fd, &opt);
    cfsetispeed(&opt, B115200);
    cfsetospeed(&opt, B115200);


    opt.c_iflag = 0;
    opt.c_cflag &= ~PARODD;     //very important; zwh
    opt.c_cflag &= ~PARENB;  /* Clear parity enable */
    opt.c_cflag &= ~CSTOPB;


    opt.c_cc[VTIME] = V_TIME;  /* unit: 1/10 second. 300ms timeout for lcd*/
    opt.c_cc[VMIN] = CC_MIN;


    opt.c_iflag &= ~INPCK;   /* Disable parity checking */
    opt.c_cflag &= ~CRTSCTS;  //NO flow control
    opt.c_cflag &= ~CSIZE;
    opt.c_cflag |= CS8;
    opt.c_cflag |= CLOCAL;
    opt.c_cflag |= CREAD;

    opt.c_oflag = 0; //输出不处理
    opt.c_lflag  = 0;  //RAW模式
    //tcdrain(pComm->fd);
    tcflush (fd, TCIOFLUSH);
    /* 0 on success, -1 on failure */

    if(tcsetattr(fd, TCSANOW, &opt) != 0 )
    qDebug("set error");
    return fd;

}

void MainWindow::update_info2()
{

    elevator_info_state2 =  elevator_uart2_read_thread->elevator_info_state;
    if(elevator_info_state2.elevator_state == 1)
    {

       elevator_uart2_read_thread->elevator_info_state.elevator_state=0;
        emit(emit_change_direction2());
        emit(emit_updatefloor2());
        emit(emit_update_spec_info2());
        //qDebug("update information 2");

    }
    else if(elevator_info_state2.elevator_state == 2)
    {

    elevator_uart2_read_thread->elevator_info_state.elevator_state=0;
    audio_info = elevator_info_state2.audio_info; //audio floor info
    if((process1->state()==QProcess::NotRunning)&&(elevator_num == "1"))
        emit(emit_play_floor_info());

    }
    else
    {
        elevator_uart2_read_thread->elevator_info_state.elevator_state=0;
    }

}

void MainWindow::update_info1()
{

    elevator_info_state1 =  elevator_uart1_read_thread->elevator_info_state;
    if(elevator_info_state1.elevator_state == 1)
    {

       elevator_uart1_read_thread->elevator_info_state.elevator_state=0;
        emit(emit_change_direction());
        emit(emit_updatefloor());
        emit(emit_update_spec_info());
        //qDebug("update information 1");

    }
    else if(elevator_info_state1.elevator_state == 2)
    {

    elevator_uart1_read_thread->elevator_info_state.elevator_state=0;
    audio_info = elevator_info_state1.audio_info; //audio floor info
    if((process1->state()==QProcess::NotRunning)&&(elevator_num == "1"))
        emit(emit_play_floor_info());
    qDebug("update information audio 1");

    }
    else
    {
        elevator_uart1_read_thread->elevator_info_state.elevator_state=0;
    }
}


void MainWindow::video_adjust()
{
    process0->write("mute 0 \n");
    qDebug("no mute");
}


void MainWindow::play_floor_info()
{

 /*
第1字节：报站目标楼层数，最高位为1时表示是地下楼层，
如0x81表示－1层；0x83表示－3层，最高位为0时表示是地上楼层，
如0x0F表示地上15层；0x64播放超载语音；0x65播放火警语音；
0x66播放暂停服务语音(视频机可以设定中文报站；
英文报站；先中文后英文混合报站)
*/
    if((audio_floor_flag == "1")||(audio_floor_flag == "2")||(audio_floor_flag == "3"))
        process0->write("mute 1 \n");

    QString str0, str1;
    if((audio_info>0) && (audio_info < 0x64))
    {
         qDebug("audio floor:%d",audio_info);
        if(audio_floor_flag == "1")
        {
            str1 = "./mplayer -ao alsa -af volume=15";
            str1 = str1 + current_dir + "/audio/dingdong.mp3 ";
            str0=str0.sprintf("/audio/f%dcn.mp3",audio_info);
            str1 = str1 + current_dir + str0;
            str0 = str1;

        }
        else if(audio_floor_flag =="2")
        {
                 //str0=str0.sprintf("mplayer -ao alsa -af volume=15 ../audio/dingdong.mp3 ../audio/f%den.mp3",audio_info);
                 str1 = "./mplayer -ao alsa -af volume=15";
                 str1 = str1 + current_dir + "/audio/dingdong.mp3 ";
                 str0=str0.sprintf("/audio/f%den.mp3",audio_info);
                 str1 = str1 + current_dir + str0;
                 str0 = str1;
        }
        else if(audio_floor_flag =="3")
        {
                //str0=str0.sprintf("mplayer -af volume=15 ../audio/dingdong.mp3  ../audio/f%dcn.mp3 ../audio/f%den.mp3 ",audio_info,audio_info);
            str1 = "./mplayer -ao alsa -af volume=15";
            str1 = str1 + current_dir + "/audio/dingdong.mp3 ";
            str0=str0.sprintf("/audio/f%dcnen.mp3",audio_info);
            //str1 = str1 + current_dir + str0;
            //str0=str0.sprintf("/audio/f%den.mp3",audio_info);
            str1 = str1 + current_dir + str0;
            str0 = str1;


        }
        else
                str0="";
    }
    else if (audio_info > 0x80)
    {
        if(audio_floor_flag == "1")
        {
            str1 = "./mplayer -ao alsa -af volume=15";
            str1 = str1 + current_dir + "/audio/dingdong.mp3 ";
            str0=str0.sprintf("/audio/fb%dcn.mp3",audio_info&0x0f);
            str1 = str1 + current_dir + str0;
            str0 = str1;

        }
        else if(audio_floor_flag =="2")
        {
                 //str0=str0.sprintf("mplayer -ao alsa -af volume=15 ../audio/dingdong.mp3 ../audio/f%den.mp3",audio_info);
                 str1 = "./mplayer -ao alsa -af volume=15";
                 str1 = str1 + current_dir + "/audio/dingdong.mp3 ";
                 str0=str0.sprintf("/audio/fb%den.mp3",audio_info&0x0f);
                 str1 = str1 + current_dir + str0;
                 str0 = str1;
        }
        else if(audio_floor_flag =="3")
        {
                //str0=str0.sprintf("mplayer -af volume=15 ../audio/dingdong.mp3  ../audio/f%dcn.mp3 ../audio/f%den.mp3 ",audio_info,audio_info);
            str1 = "./mplayer -ao alsa -af volume=15";
            str1 = str1 + current_dir + "/audio/dingdong.mp3 ";
            str0=str0.sprintf("/audio/fb%dcnen.mp3",audio_info&0x0f);
            //str1 = str1 + current_dir + str0;
            //str0=str0.sprintf("/audio/f%den.mp3",audio_info);
            str1 = str1 + current_dir + str0;
            str0 = str1;


        }
        else
                str0="";

        //str0.sprintf("./mplayer ../audio/fb%dcn.mp3",audio_info&0x0f);
    }
    else if(audio_info == 0x64)
    {}
    else if (audio_info == 0x65)
    {}
    else if (audio_info == 0x66)
    {}
    else
        str0=str0.sprintf("./mplayer");

    if(process1->state()==QProcess::NotRunning)
          process1->start(str0);

    //QTimer::singleShot(6000, this, SLOT(video_adjust()));


}

void MainWindow::read_uart_buf()
{

    char buff[1024];
    int numBytes;

    numBytes = port->bytesAvailable();
    if(numBytes > 0)
    {
    if(numBytes > 128)
        numBytes = 128;

    int i = port->read(buff, numBytes);
    if (i != -1)
        buff[i] = '\0';
    else
        buff[0] = '\0';
    //QString msg = buff;

    //received_msg->append(msg);
    //received_msg->ensureCursorVisible();
    qDebug("bytes available: %d", numBytes);
    //qDebug("received: %s", buff);
    }


}


void MainWindow::parse_config()
{

    QString tmp_line = "/file/config.conf";
    tmp_line =current_dir + tmp_line;
    QFile file(tmp_line);

    week_dis_flag =1;
    audio_floor_flag = "4";
    elevator_info_uart_1 = "/dev/ttyS0";
    elevator_info_uart_2 = "/dev/ttyS1";
    elevator_num = "1";
    text_uart = "/dev/ttyS2";
    display_pos = "1";

     if ( file.open(QIODevice::ReadOnly) ) {
         QTextStream stream( &file );
         QString line;
         QStringList list0;
         QChar *char0;
         int tt;

         stream.setCodec("UTF-8");
#if 1
         while ( !stream.atEnd()) {
             line = stream.readLine(); // 不包括“\n”的一行文本
             char0 = line.data();
             if(*char0 == '#')
                 continue;
             line = line.remove(" ");
             list0 = line.split(":");
             tt = list0.count();
             if(list0.at(0) == "week_dis")
             {

                 for(tt=0;tt<list0.count();tt++)
                     if(tt==1)
                     {
                        line = list0.at(tt);
                        if(line == "1")
                            week_dis_flag = 1;
                        else
                            week_dis_flag = 2;
                     }
             }

           else if(list0.at(0) == "audio_floor")
             {

                 for(tt=0;tt<list0.count();tt++)
                     if(tt==1)
                     {
                        line = list0.at(tt);
                        if(line == "3")
                            audio_floor_flag = "3";
                        else if(line == "2")
                            audio_floor_flag = "2";
                        else if(line == "1")
                            audio_floor_flag = "1";
                        else
                            audio_floor_flag = "4";
                     }
             }
           else if(list0.at(0) == "ele1_uart")
                       {

                           for(tt=0;tt<list0.count();tt++)
                               if(tt==1)
                               {
                                 line = list0.at(tt);
                                 elevator_info_uart_1 = line;
                               }
                         }
             else if(list0.at(0) == "ele2_uart")
                         {

                             for(tt=0;tt<list0.count();tt++)
                                 if(tt==1)
                                 {
                                   line = list0.at(tt);
                                   elevator_info_uart_2 = line;
                                 }
                           }
             else if(list0.at(0) == "elevator_num")
                         {
                             for(tt=0;tt<list0.count();tt++)
                                 if(tt==1)
                                 {
                                   line = list0.at(tt);
                                   elevator_num = line;
                                 }
                           }
           else if(list0.at(0) == "text_uart")
                       {

                           for(tt=0;tt<list0.count();tt++)
                               if(tt==1)
                               {
                                 line = list0.at(tt);
                                 text_uart = line;
                               }
                       }
             else if(list0.at(0) == "serial_num")
                         {

                             for(tt=0;tt<list0.count();tt++)
                                 if(tt==1)
                                 {
                                   line = list0.at(tt);
                                   serial_num = line;
                                 }
                         }
             else if(list0.at(0) == "display_pos")
                         {

                             for(tt=0;tt<list0.count();tt++)
                                 if(tt==1)
                                 {
                                   line = list0.at(tt);
                                   display_pos = line;
                                 }
                         }
           else
                   line="";
         }
#endif
         file.close();
     }
     else
     {
       qDebug("open caption fail!\n");
       week_dis_flag =1;
       audio_floor_flag = "4";
       elevator_info_uart_1 = "/dev/ttyS0";
       elevator_info_uart_2 = "/dev/ttyS1";
       elevator_num = "1";
       text_uart = "/dev/ttyS2";
       display_pos = "1";

     }

}


void MainWindow::update_video_player()
{

    QString string_tmp;
    int tmp_i;
    if(video_files.count() > 0)
    {
        if(process0->pid() == 0)
        {
            video_counter++;
            if(video_counter>=video_lines.count())
                video_counter =0;

            tmp_i = video_counter;
            string_tmp=video_lines.at(tmp_i);
            process0->start(string_tmp);
            qDebug("%s", qPrintable(string_tmp));
            QTimer::singleShot(300,this,SLOT(seek_video()));
        }
    }

}

void MainWindow::video_codec_check()
{



 #if 1
    QFile file0("/tmp/tmp.txt");
    if(file0.exists())
        QFile::remove("/tmp/tmp.txt");
    QProcess tmp_process;
    QStringList tmp_list = video_lines;
    QString tmp_line;
    qint32 tmp_num = tmp_list.count();
    qint32 tmp_i;
    for(tmp_i =0;tmp_i<tmp_num;tmp_i++)
    {
        tmp_line = tmp_list.at(tmp_i);
        tmp_line = "./checkcodec.sh " + tmp_line;
        tmp_process.start(tmp_line);
        tmp_process.waitForFinished(30000);
        qDebug("codec check");
    }

        video_lines.clear();
    QFile file2("/tmp/tmp.txt");
     if ( file2.open(QIODevice::ReadOnly) ) {
           QTextStream stream( &file2);
           QString line;
           QChar *char0;
         stream.setCodec("UTF-8");
#if 1
         while ( !stream.atEnd()) {
             line = stream.readLine(); // 不包括“\n”的一行文
             //line = line.remove(" ");
             //qDebug("line %s",line.at(0).toAscii());
             if(line == "")
                 continue;
             char0 = line.data();
             if((*char0 == '#') && (*(char0+1) == '#'))
                   continue;
             else
             {
                   video_lines += line;
             }
         }
#endif
         qDebug("video_list number:%d ",video_lines.count());
         file2.close();
     }
     else
       qDebug("open video_list fail!\n");



    tmp_list=video_lines;

    if(tmp_list.count()>0)
        {
            video_lines.clear();
            //qDebug("%s", qPrintable(video_lines.at(0)));
            QString string_tmp = "./mplayer ";
            //QString string_tmp1 = " -ao alsa  -af volume=20 -zoom -quiet -slave -loop 0 ";
            QString string_tmp1 = " -ao alsa -af volume=10 -zoom -quiet -slave ";

            qint32 tmp0;
            tmp0=ui->frame->winId();
            string_tmp +=string_tmp1;
            string_tmp +=" -wid " + QString::number(tmp0);
            string_tmp += " ";
            for(tmp_i =0;tmp_i<tmp_list.count();tmp_i++)
            {
                tmp_line = tmp_list.at(tmp_i);
                string_tmp1=string_tmp;
                string_tmp1 += tmp_line;
                video_lines += string_tmp1;
                string_tmp1 = "";
            }

            tmp_i = 0;
            tmp_line = video_lines.at(tmp_i);
            qDebug("video_list 111 number:%d ",video_lines.count());
            //qDebug("video_list number:%s ",tmp_line.toStdString());
            //qDebug()<<tmp_line;
            qDebug("%s", qPrintable(tmp_line));
            video_counter = 0;
            process0->start(tmp_line);
            QTimer::singleShot(300,this,SLOT(seek_video()));
        }


#endif

}
